/**
 * 
 */
package org.lookouster.nextAgadir.pointageBureau.ui;

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import org.lookouster.nextAgadir.pointageBureau.outils.BDConnect;
import org.lookouster.nextAgadir.pointageBureau.outils.BDFunctions;

/**
 * @author Lookouster
 *
 */
/*
 * MatriculeSetter.java
 *
 * Created on 6 mai 2008, 22:56
 */


/**
 *
 * @author  Lookouster
 */

public class MatriculeSetter extends javax.swing.JDialog {
    
    /** Creates new form MatriculeSetter */
	
	/**
	 * Constructor
	 * action=1->Deactivate mode
	 * action=2->Delete mode
	 */
    public MatriculeSetter(java.awt.Frame parent, int action) {
        super(parent, true);
        this.setLocation(50, 50);
        initComponents();
        if(action==1)
        	setDisableMode();
        else if(action==2)
        	setDeleteMode();
        else
        	this.setVisible(false);
        
    }
    
    public void setDisableMode()
    {
    	jLabel1.setText(UICommons.MSDis);
    	jButton1.setText(UICommons.MSBtnDea);
    	jButton1.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				try {
					disableIt();
				} catch (HeadlessException e) {
					// TODO Auto-generated catch block
					//e.printStackTrace();
					showDelFail();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					//e.printStackTrace();
					showDelFail();
				}
			}
    		
    	});
    }
    
    public void showDelFail()
    {
    	JOptionPane.showMessageDialog(this,UICommons.MSNotifDelFail);
    }
    
    public void setDeleteMode()
    {
    	jLabel1.setText(UICommons.MSDel);
    	jButton1.setText(UICommons.btnDel);
    	jButton1.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				try {
					deleteIt();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					//e.printStackTrace();
					showDisFail();
				}
			}
    		
    	});
    }
    
    
    public void showDisFail()
    {
    	JOptionPane.showMessageDialog(this,UICommons.MSNotifDisFail);
    }
    
    /**
     * delete the employee referenced by matricule in JtextField
     * @throws Exception 
     * @throws HeadlessException 
     */
    public void deleteIt() throws  Exception
    {
    	String matricule=txtMatricule.getText().trim();
    	if(matricule.length()!=0)
    	{
    		if(BDFunctions.existEmploye(matricule))
    		{

    				BDFunctions.deleteEmp(matricule);
    				JOptionPane.showMessageDialog(this,UICommons.MSNotifDelOk);
    				txtMatricule.setText("");
    		}
    		else
    			JOptionPane.showMessageDialog(this,UICommons.notifNoMatch);
    	}
    	else
    		
    		JOptionPane.showMessageDialog(this,UICommons.notifEmpty);
    	
    }
    
    /**
     * disable the employee referenced by matricule in JtextField
     * @throws Exception 
     */
    public void disableIt() throws Exception
    {
    	String matricule=txtMatricule.getText().trim();
    	if(matricule.length()!=0)
    	{
    		if(BDFunctions.existEmploye(matricule))
    		{
    			if(BDFunctions.checkMatriculeExists(matricule))
    			{
    				BDConnect bdc=new BDConnect();
    				BDFunctions.getEmployeFromMat(matricule).disable(bdc);
    				bdc.shutdown();
    				JOptionPane.showMessageDialog(this,UICommons.MSNotifDisOk);
    				txtMatricule.setText("");
    			}
    			else
    				JOptionPane.showMessageDialog(this,UICommons.MSNotifDis);
    		}
    		else
    			JOptionPane.showMessageDialog(this,UICommons.notifNoMatch);
    	}
    	else
    		
    		JOptionPane.showMessageDialog(this,UICommons.notifEmpty);
    }
    
   
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtMatricule = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        

        
        //jLabel1.setText("jLabel1.text"); 
        

        txtMatricule.setText("");
        

        jLabel2.setText(UICommons.MSMat); 
        

        //jButton1.setText("jButton1.text");
        

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(jLabel2)
                        .addGap(38, 38, 38)
                        .addComponent(txtMatricule, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(40, 40, 40)
                        .addComponent(jButton1)))
                .addContainerGap(86, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtMatricule, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        pack();
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                MatriculeSetter dialog = new MatriculeSetter(new javax.swing.JFrame(),2);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField txtMatricule;
    // End of variables declaration
    
}

